<?php
class action_intall extends database_mysql
{
    public $layout = NULL;
    public function __construct()
    {
        $this->view = new system_view();
        $this->model = new model__action_model();
        $this->rt = new handle_route();   
        $this->_seokey = $this->rt->rt_get_all_val_seo();
    }
    
    public function index_controller()
    {
        $html = '';
        $_seokey = $this->rt->rt_get_all_val_seo();
        $action = isset($_seokey['2'])?$_seokey['2']:'';
        
        switch($action){
            case '':
                $html = $this->add_controller();
            break;
            case 'ajax':
                $html = $this->ajax_controller();
            break;
        }
        
        
        
        return $html;
    }
    
    public function add_controller(){
        $error = '';
        $success = '';
        $strHtml = $this->view->view_display('add');
        $arrHtml = $this->view->view_get_block_html($strHtml);
        $app = $this->_seokey[0];
        $list_com = $this->model->md_cm_get_list_com($app);
        
        $strOkHtml = '';
        if(!empty($list_com)){
            foreach($list_com as $value){
                $strOkHtml .= $this->view->view_assign($arrHtml['com'],array(
                                                                            'com_value'                 => $value['Com'],
                                                                            'com_id'                    => $value['ID']
                                                                        ));
            }
        }else{
            $error = 'Hiện tại không có Com trong database!';
        }
        
        if(isset($_POST) && !empty($_POST['com']) && !empty($_POST['action'])){
            $com_id = $_POST['com'];
            $action_id = $_POST['action'];
            $app = $_POST['app'];
            $com_name = $this->model->md_cm_get_com_from_id($com_id, $app);
            if(!empty($com_name)){
                $com_name = $com_name['Com'];
                $com_path = realpath('../') . '/app/' . $app . '/controller/' . $com_name . '.php';
                
                $action_name = $this->model->md_cm_get_act_from_id($action_id);
                $action_name = $action_name['ActName'];
                
                /************create file view************/
                if(isset($_POST['view']) && $_POST['view'] == 'view'){
                    
                }
                require_once($com_path);
                $com_class_curent = get_class_methods('controller_'.$com_name);
                if(!in_array($action_name.'_action', $com_class_curent)){
                    $content_source = file_get_contents($com_path);
                    $content_source = trim($content_source);
                    $content_source = trim($content_source, "}");
                    $dir_view_action_tmp = realpath('./temp/action/demo.php');
                    $content_view_action_tmp = file_get_contents($dir_view_action_tmp);
                                       
                    $content_view_action_tmp = str_replace('demo',$action_name,$content_view_action_tmp);
                    $content_source = $content_source . $content_view_action_tmp;
                    $fh = fopen($com_path, 'w+');
                    fwrite($fh, $content_source);
                    fclose($fh);  
                    
                    if(isset($_POST['view']) && $_POST['view'] == 'view'){
                        
                        $dir_view = realpath('../app/'.$app.'/view/temp/default/');
                        $dir_view_cur = $dir_view.'/'.$com_name;
        
                        $file_tmp = realpath('./temp/com/view/temp/') . '/index.phtml';
                        $content_source = file_get_contents($file_tmp);
                        $content_source = str_replace('demo',$com_name,$content_source);
                        $file_view_cur = $dir_view_cur . '/' . $action_name . '.phtml';
                        $fh = fopen($file_view_cur, 'w+');
                        fwrite($fh, $content_source);
                        fclose($fh);
                    }
                      
                }
                $data = array(
                    'ActID'=>$action_id,
                    'ComID'=>$com_id,
                    'Status'=>'true'
                );
                $check_action = $this->model->model_action_check_action($action_id, $com_id);
                if(empty($check_action)){
                    $this->model->md_cm_insert_action($data);    
                }
                $success ='Action đã thêm thành công';
                
            }
        }

        $strHtmlContent = $this->view->view_assign_block($strHtml, array(
                                                                    'list_com'=>$strOkHtml));
        $strHtmlContent = $this->view->view_assign($strHtmlContent,array('error'=>$error, 
                                                                         'app'=>$app,
                                                                         'success'=>$success ));
        return $strHtmlContent;

    }
    
    public function ajax_controller(){
        $html = 'no-data';
        $this->layout = 'ajax';
        $action = isset($_POST['action'])?$_POST['action']:'';
        $app = isset($_POST['app'])?$_POST['app']:'';
        $com_name = isset($_POST['com_name'])?$_POST['com_name']:'';
        $com_id = isset($_POST['com_id'])?$_POST['com_id']:'';
        $com_path = realpath('../') . '/app/' . $app . '/controller/' . $com_name . '.php';
        if(!file_exists($com_path)){
            $path_com_create = realpath('../') . '/installs/system/com.php';
            require_once($path_com_create);
            $com_create = new com_intall();
            $com_create->cteate_file_controller($com_name);
        }
        require_once($com_path);
        $com_class_curent = get_class_methods('controller_'.$com_name);
        $action_list = $this->model->md_cm_get_list_action($com_id);
        $action_list_tmp = $action_list;
        if(!empty($action_list_tmp)){
            $html = '';
            foreach($action_list_tmp as $key=>$value){
                $action_name = $value['ActName'].'_action';
                if(in_array($action_name, $com_class_curent)){
                    unset($action_list[$key]);
                }else{
                    $html .= '<option value="'.$value['ID'].'">'.$value['ActName'].'</option>';
                }
            }
        }
        return $html;
    }
}

